<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>A</title>
	<link href="__PUBLIC__/css/iconfont/material-icons.css" rel="stylesheet">
	<link rel="stylesheet" href="__PUBLIC__/materialize/css/materialize.min.css" media="screen,projection" />
    <link href="__PUBLIC__/css/bootstrap.css" rel="stylesheet" />
	<link href="__PUBLIC__/css/custom-styles.css" rel="stylesheet" />
	<link rel="stylesheet" href="__PUBLIC__/js/bootstrap-select/dist/css/bootstrap-select.min.css" type="text/css">
	<link rel="stylesheet" href="__PUBLIC__/js/DataTables-1.10.0/media/css/jquery.dataTables.min.css">

	<script src="__PUBLIC__/js/jquery-3.4.1.js"></script>
	<script src="__PUBLIC__/js/bootstrap.min.js"></script>
	<script src="__PUBLIC__/js/layer/src/layer.js"></script>
	<script src="__PUBLIC__/js/laydate/laydate.js"></script>
	<script type="text/javascript" src="__PUBLIC__/js/bootstrap-select/dist/js/bootstrap-select.min.js"></script>
	<script src="__PUBLIC__/js/tool.js"></script>
	<script src="__PUBLIC__/js/paigusu.min.js"></script>

	<style type="text/css">
		body{background:#fff;font-size:12px}
		table tbody tr td{
			padding:0 !important;
			height:32px !important;
		}
		.edit-default .col{
			text-align:center;
			height:32px;line-height:32px;
			padding:10px;
			margin-bottom:20px
		}

		label{
			color:#4b4b4b;
			font-size:12px !important
		}
		
	</style>
	
</head>

<body>

<div class = 'row'>

	<div class = 'col s12'>
		<table class = 'dataTable centered cell-border table-small no-select'>
			<thead>
				<tr><th></th><th>名称</th><th>浏览</th><th>编辑</th><th>隐藏</th><th>必填</th><th>默认值</th></tr>
			</thead>
			<tbody id = 'tbody'>
				{$tbody|raw}
			</tbody>
		</table>
	</div>

</div>

<script>
	var app = {

		auth : {$auth|raw},

		flow_id : {$Request.get.flowid},

		node_id : '{$Request.get.node}',

		defaultType : {$defaulttype|raw},

		save : function(){
			let o = {};

			o.flow_id = app.flow_id;
			o.node_id = app.node_id;
			
			$('#tbody tr').each(function(){
			
				let i = $(this).data('field');
				app.auth[i].a = parseInt($("input[name='"+i+"']:checked").val());
				app.auth[i].m = app.auth[i].a != 1?0:($(this).find("input[type='checkbox']").prop('checked')?1:0);

				switch (app.auth[i].b){
					case 'checkbox' : 
						app.auth[i].d = $(this).children().eq( $(this).children().length - 1).text() == ''?0:1;
					break;
					case 'radio' :
						app.auth[i].d = $(this).children().eq( $(this).children().length - 1).text() == ''?0:1;
					break;
					case 'enum' :
						if( $(this).children().eq(6).text() != '' ){
							app.auth[i].d = $(this).children().eq(6).data('d');
						}else{
							app.auth[i].d = '';
						}
					break;
					default:
				}

				//oo.push(tmp);
			});
		
			//o.auth = JSON.stringify(oo);

			o.auth = JSON.stringify( app.auth );

			$.post('__APP__/F/edit_flow_auth',o,function(d){
				if(d.status == 's'){
					parent.layer_success();
					var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
					parent.layer.close(index); //再执行关闭   
				}
			});
			
		},

		ini: function(){

			$('#tbody td.checked').click(function(){	
				if( !$(this).parent().children().eq(3).find('input').is(':checked') ) return false;
				let type = $(this).parents('tr').data('type');
				let that = this;
				let label = $(this).parents('tr').children().eq(1).text();
				let field = $(this).parents('tr').data('field');
				if( type == 'checkbox'){
					if($(this).text() == ''){
						$(this).text( '√' );
						app.auth[field].d = 1;
					}else{
						$(this).text( '' );
						app.auth[field].d = 0;
					}	
				}else if( type == 'radio' ){
					let group = $(this).data('group');
					$('.' + group).text( '' );
					$(this).text( '√' );
				}else if ( type == 'enum'){
					let enum_id = $(this).parents('tr').data('enum_id');
					let loadIndex = layer.load(2);
					$.post('__APP__/PublicGet/get_enum_detail?enum_id='+enum_id,{},function(d){
						let tmp = "<div style = 'border-bottom:1px solid #d5d5d5;padding:10px'><select id = 'enum_default' class = 'browser-default'><option value = ''></option>";
						for( let i = 0; i < d.length; i++){
							tmp += "<option value = '"+d[i]['id']+"'>"+d[i]['name']+"</option>";
						}	
						tmp += "</select></div>";
						layer.close(loadIndex);
						layer.open({
							//skin: 'layer-edit-container',
							title:"<span style = 'font-size:12px'>选择默认值</span>",
							area: ['400px','150px'],
							offset:'30%',
							shadeClose:true,
							isOutAnim: false ,
							maxmin: true,
							type: 1, 
							content:tmp,
							btn : [ '确定'],
							success : function(){
								if( $(that).data('d') ){
									$('#enum_default').val( $(that).data('d') );
								}
							},
							yes : function(index){
								let k = $('#enum_default').val();
								let v = $('#enum_default').find('option:selected').text();
								$(that).text(v);
								$(that).data('d',k);
								layer.close(index);
							}
						});

					});

				}else if ( type == 'varchar' ){

					let typeSelect = "<select class = 'browser-default' id = 'xtz-k' style = 'vertical-align:middle'><option value = ''></option>";
					$.each(app.defaultType,function(k,v){
						
						if( k >= 100 ) typeSelect += "<option value = '"+k+"'>"+v+"</option>";
					});
					typeSelect += "</select>";

					let content = "<div class = 'row edit-default'><div class = 'col s4'><p style = 'display:inline-block' ><input name='group1' type='radio' id='sgz'  data-type = '0' "+(app.auth[field].t?"":"checked")+" /><label for='sgz'>手工值</label></p></div><div class = 'col s6'><input type = 'text' class = 'aya-input' id = 'sgz-k' /></div><div class = 'col s4'><p style = 'display:inline-block'><input name='group1' type='radio' id='xtz'  data-type = '1' "+(app.auth[field].t?"checked":"")+" /><label for='xtz'>系统值</label></p></div><div class = 'col s6'>"+typeSelect+"</div></div>";

					layer.open({
						title:"<span style = 'font-size:12px'>编辑默认值（ "+label+" ）</span>",
						area: ['400px','300px'],
						offset:'10%',
						shadeClose:true,
						isOutAnim: false ,
						maxmin: true,
						type: 1, 
						content:content,
						btn : [ '确定'],
						success : function(){
							$("input[type='radio']").click(function(){
								let type = $(this).data('type');
								if(type == '0'){  // 手工值
									$('#xtz-k').val('').prop('disabled',true);
									$('#sgz-k').prop('disabled',false);
								}else{
									$('#sgz-k').val('').prop('disabled',true);
									$('#xtz-k').prop('disabled',false);
								}
							});

							
							if( !app.auth[field].t ){
							
								$('#sgz-k').val( app.auth[field].d?app.auth[field].d:'' );
							}else{
								
								$('#xtz-k').val(  app.auth[field].t );
							}
						},
						yes : function(index){
							app.auth[field].d = $.trim($('#sgz-k').val());
							app.auth[field].t = $('#xtz-k').val();

							if( $('#sgz').is(':checked') ){
								$(that).parents('tr').children().eq(6).text( app.auth[field].d );
							}else{
								$(that).parents('tr').children().eq(6).text( $('#xtz-k').find('option:selected').text() );
							}
							layer.close(index);
						}

					});
				}else if(type == 'person' || type == 'department' || type == 'post' ){
					let content = "<div class = 'row edit-default'><div class = 'col s4'>默认值</div><div class = 'col s6'><select class = 'browser-default' id = 'system-select'><option value = ''></option><option value = '1'>登陆者用户</option></select></div><div class = 'col s2'></div><div class = 'col s4'>可以编辑</div><div class = 'col s8' style = 'text-align:left'><input id = 'system-select-can-change' type = 'checkbox' class = 'aya-checkbox' /></div></div>";
					layer.open({
						title:"<span style = 'font-size:12px'>编辑默认值（ "+label+" ）</span>",
						area: ['400px','250px'],
						offset:'10%',
						shadeClose:true,
						isOutAnim: false ,
						maxmin: true,
						type: 1, 
						content:content,
						btn : [ '确定'],
						success : function(){
							$('#system-select').change(function(){
								if( $(this).val() == ''){
									$('#system-select-can-change').prop('disabled',true).prop('checked',true);
								}else{
									$('#system-select-can-change').prop('checked',false).prop('disabled',false);
								}
							});
							$('#system-select').val( app.auth[field].t );
							$('#system-select-can-change').prop('checked',app.auth[field].n?true:false );
							
						},
						yes : function(index){
							app.auth[field].n = $('#system-select-can-change').is(':checked')?1:0;
							app.auth[field].t = $('#system-select').val();
							$(that).parents('tr').children().eq(6).text( $('#system-select').find('option:selected').text() );
							layer.close(index);
						}
					});
				}
			});

			$('#tbody input[type=radio]').click(function(){
				let field = $(this).parents('tr').data('field');
				app.auth[field].a = $(this).val();
				if( $(this).val() == 1){
					$(this).parents('tr').find( 'input[type=checkbox]' ).attr('disabled',false);
				}else{
					let type = $(this).parents('tr').data('type');
					if(type == 'checkbox' || type == 'radio'){
						$(this).parents('tr').children().eq( $(this).parents('tr').children().length - 1).text( '' );
					}
					$(this).parents('tr').find( 'input[type=checkbox]' ).attr('disabled',true);
					app.auth[field].d = '';
					app.auth[field].t = '';
					
				}
				
			});

			$('#tbody input[type=checkbox]').click(function(){
				let field = $(this).parents('tr').data('field');
				app.auth[field].m = $(this).is(':checked')?1:0;
			});
		
			if( app.auth.no ){       // 没有权限记录
				if(app.node_id == 'creator'){
					$('#tbody tr').each(function(){
						$(this).children().eq(3).find('input').attr('checked',true);
					});
				}else{
					$('#tbody tr').each(function(){
						$(this).children().eq(2).find('input').attr('checked',true);
						$(this).find("input[type='checkbox']").attr('disabled',true);
					});
				}
			}else{

				/*
				$.each(app.auth,function(i,v){
					$( "input[name='"+v.i+"'][type='radio'][value='"+v.a+"']").attr('checked',true);
					if( v.a == 1){
						if( v.m == 1 ){
							$("input[type='checkbox'][name='"+v.i+"']").attr('checked',true);
						}
					}else{
						$("input[type='checkbox'][name='"+v.i+"']").attr('disabled',true);
					}
				});
				*/

			

				$('#tbody tr').each(function(){
					let field = $(this).data('field');
					let type = $(this).data('type');
					if( type == 'relation' ){
						$(this).find("input[type='radio']").attr('checked',false).attr('disabled',true);
						$(this).find("input[type='checkbox']").attr('checked',false).attr('disabled',true);
					}else{
						let a = app.auth[field].a ;
						$(this).children().eq( parseInt(a) + 2 ).find('input').attr('checked',true);
						$(this).children().eq(5).find('input').attr('checked',app.auth[field].m?true:false);
						
						
					}
				});

			}

			

			//log(app
		}
		
	}
	
	
	app.ini();

</script>

</body>

</html>
